<template>
  <div class="entrances">
    <u-cell-group v-if="listData.length > 0">
      <u-cell-item
        v-for="(item, index) in listData"
        :key="index"
        :title="item.warpNmae ? item.warpNmae : item[fieldName] || ''"
        @click="onCellItem(item)"
      />
    </u-cell-group>

    <div class="save" @tap="confirm">新增{{ moduleData.moduleName }}</div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getEmpFieldValue } from "@/services/employee";
export default {
  components: {},

  data: () => ({
    listData: [],
    moduleData: {},
    fieldName: ""
  }),
  computed: {
    ...mapState(["site", "employee", "user"]),
    verified() {
      return this.site.verified;
    }
  },
  // groupField
  // 页面周期函数--监听页面加载
  onLoad(option) {
    this.moduleData = uni.getStorageSync("module_data");
    uni.setNavigationBarTitle({
      title: this.moduleData.moduleName
    });
  },
  // 页面周期函数--监听页面初次渲染完成
  onReady() {},
  // 页面周期函数--监听页面显示(not-nvue)
  onShow() {
    this.getEmpFieldValues(uni.getStorageSync("module_data").moduleCode);
  },
  // 页面周期函数--监听页面隐藏
  onHide() {},
  // 页面周期函数--监听页面卸载
  onUnload() {},
  // 页面处理函数--监听用户下拉动作
  onPullDownRefresh() {
    uni.stopPullDownRefresh();
  },
  // 页面处理函数--监听用户上拉触底
  onReachBottom() {},
  methods: {
    confirm() {
      uni.navigateTo({
        url: "/employee/entrances?sign=1"
      });
    },
    onCellItem(data) {
      uni.setStorageSync("all_field", data);
      uni.navigateTo({
        url: "/employee/entrances?sign=2"
      });
    },
    getEmpFieldValues(moduleCode) {
      getEmpFieldValue({
        empInfoId: this.site.employeeId,
        companyId: this.site.companyId,
        moduleCode: moduleCode
      })
        .then(res => {
          uni.getStorageSync("module_data").fieldList.forEach(element => {
            if (element.groupField) {
              if (element.fieldType === 1 || element.fieldType === "1") {
                let options = [];
                if (element.dropDownEnumList) {
                  options = element.dropDownEnumList;
                }
                if (element.dropDownArray) {
                  element.dropDownArray.forEach(element => {
                    options.push({
                      value: element,
                      label: element
                    });
                  });
                }

                res.listData.forEach((esys, index) => {
                  if (esys[element.fieldCode]) {
                    options.forEach(esy => {
                      if (esy.value == esys[element.fieldCode]) {
                        res.listData[index].warpNmae = esy.label;
                      }
                    });
                  }
                });
              } else {
                this.fieldName = element.fieldCode;
              }
            }
          });
          this.listData = res.listData;
        })
        .catch(err => {
          console.log("debug log --> ", err);
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.entrances {
  width: 100%;
  background-color: #fafbfb;
}
.save {
  margin-top: 12px;
  width: 100%;
  height: 50px;
  line-height: 50px;
  text-align: center;
  background-color: #ffffff;
  font-size: 14px;
  color: $theme;
  z-index: 10;
}
</style>
